/**
 * @author Shen Yi - bm2736892@gmail.com
 */

function load(name)
{
	loadInfo("载入中.....")
	$.post("mods/" + name + "/config.json", {}, function(json){
		var config = eval("(" + json + ")");
		//loadLight(config);
		for(var modelName in config.model){
			//TODO : the camera name should not be write directly
			//SceneManager.loadModel(modelName, "main_camera");
		}
		loadMaterial(config, function(){
			bindMaterialToPrimitive(config);
			loadInfo("载入完毕......");
		});
	})
}

function loadLight(config)
{
	var lights = config.light;
	for (var i = 0; i < lights.length; i++) {
		var light = SceneManager.createLight(lights[i].name, lights[i]);
		//TODO : the camera name should not be write directly
		light.enableShadowMap("main_camera", "main_camera");
		light.bindToTransform("main_camera");
	}
}

function loadMaterial(config, callback)
{
	MaterialManager.reset();
	loadInfo("载入材质中.....");
	var materialLoadCount = 0;
	for(var materialName in config.material){
		materialLoadCount++;
		MaterialManager.createFxMaterial(materialName, config.material[materialName], function(material){
			//TODO : the camera name should not be write directly
			MaterialManager.bindCamera(material.name, "main_camera");
			materialLoadCount--;
			if(materialLoadCount == 0){
				callback();
			}
		});
	}
}

function bindMaterialToPrimitive(config)
{
	for(var primitiveName in config.primitive){
		var materialName = config.primitive[primitiveName];
		SceneManager.getPrimitive(primitiveName).material = MaterialManager.getMaterial(materialName);
	}
}

function loadInfo(info)
{
	$("#debug").html(info);
}